package com.sky.financelake.retrofit;

import androidx.lifecycle.MutableLiveData;

import com.sky.financelake.bean.dto.SimpleStockDto;
import com.sky.financelake.bean.dto.StockDto;
import com.sky.financelake.bean.param.StockParam;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class StockRepository {

    private static StockRepository stockRepository;

    public static StockRepository getInstance() {
        if (stockRepository == null) {
            stockRepository = new StockRepository();
        }
        return stockRepository;
    }

    private Api api;

    public StockRepository() {
        api = RetrofitService.cteateService(Api.class);
    }

    public void getStockByCode(MutableLiveData data, String code) {
        api.getStockByCode(code).enqueue(new Callback<ApiResponse<StockDto>>() {
            @Override
            public void onResponse(Call<ApiResponse<StockDto>> call, Response<ApiResponse<StockDto>> response) {
                if (response.isSuccessful()) {
                    data.setValue(response.body());
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<StockDto>> call, Throwable t) {
                data.setValue(null);
            }
        });
    }

    public void getStockPagingList(MutableLiveData data, StockParam param) {
        api.getStockPagingList(param).enqueue(new Callback<ApiResponse<List<StockDto>>>() {
            @Override
            public void onResponse(Call<ApiResponse<List<StockDto>>> call, Response<ApiResponse<List<StockDto>>> response) {
                if (response.isSuccessful()) {
                    data.setValue(response.body());
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<List<StockDto>>> call, Throwable t) {
                data.setValue(null);
            }
        });
    }


    public void getRelativeStockByPlates(MutableLiveData data, String plates) {
        api.getRelativeByPlates(plates).enqueue(new Callback<ApiResponse<List<SimpleStockDto>>>() {
            @Override
            public void onResponse(Call<ApiResponse<List<SimpleStockDto>>> call, Response<ApiResponse<List<SimpleStockDto>>> response) {
                if (response.isSuccessful()) {
                    data.setValue(response.body());
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<List<SimpleStockDto>>> call, Throwable t) {
                data.setValue(null);
            }
        });
    }
}
